//
//  BaseTableAndSearchViewController.m
//  Bus_iPhone
//
//  Created by antunnel on 12-7-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "BaseTableAndSearchViewController.h"

@interface BaseTableAndSearchViewController ()

@end

@implementation BaseTableAndSearchViewController


@synthesize tableView=_tableView;
@synthesize searchBar=_searchBar;
@synthesize list=_list;


-(void)dealloc{

    [_tableView release];
    [_searchBar release];
    [_list release];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:BusDataManagerDataBaseFileChangeNSNotification object:nil];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:@"BaseTableAndSearchViewController" bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(dataBaseChange) name:BusDataManagerDataBaseFileChangeNSNotification object:nil];
    
    BusSetting *setting=[BusSetting defaultSetting];
    if (setting.busDataFilePath==nil) {
        
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"数据库没有设置,是否去设置数据库?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置", nil];
        [alert show];
        [alert release];
        isGoSetting=YES;
    }else {
        [self fuzzySearchWithWord:@""];
    }
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


-(void)setList:(NSArray *)list{

    if ([list isEqualToArray:_list]) {
        return;
    }
    
    [_list release];
    _list=[list retain];
    
    [self.tableView reloadData];
}

// 数据库文件发生变化

-(void)dataBaseChange{
    
    NSArray *tempNewViewController=[NSArray arrayWithObject:self];
    [self.navigationController setViewControllers:tempNewViewController animated:YES];
    [self fuzzySearchWithWord:@""];
}

// 模糊搜索 
     
-(void)fuzzySearchWithWord:(NSString *)text{

    // searhc bar text change do action
}

#pragma mark -UIScrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    
    if (scrollView.isDragging&&!scrollView.isDecelerating) {
        
        [[NSNotificationCenter defaultCenter]postNotificationName:CXSearchBarResignFirstResponderNotication object:nil];
    }
}

#pragma mark -UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _list.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [[NSNotificationCenter defaultCenter]postNotificationName:CXSearchBarResignFirstResponderNotication object:nil];
}

#pragma mark -CXSearchBarDelegate

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{

    if (searchBar.text.length==0) {
        
        [searchBar setShowsCancelButton:YES animated:YES];
    }else {
        [searchBar setShowsCancelButton:NO animated:YES];
    }
    
    [self fuzzySearchWithWord:searchText];
}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{

    [searchBar resignFirstResponder];
}

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{

    if (searchBar.text.length==0) {
    
        [searchBar setShowsCancelButton:YES animated:YES];
    }else {
        [searchBar setShowsCancelButton:NO animated:YES];
    }
}

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{

    [searchBar setShowsCancelButton:NO animated:YES];
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{

    [searchBar resignFirstResponder];
}


#pragma mark -UIAlertViewDelegate

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    
    if (isGoSetting&&buttonIndex==1) {
        self.tabBarController.selectedIndex=3;
    }
    isGoSetting=NO;
}
@end
